﻿package com.mttang.core {
	
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.*;
	import flash.net.SharedObject;
	import flash.net.SharedObjectFlushStatus;
	import flash.events.NetStatusEvent;
	import flash.net.NetConnection;
	import flash.ui.Mouse;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.utils.getDefinitionByName;
	import flash.events.KeyboardEvent;
	//import flash.media.SoundMixer;
	
	import classes.GameSetting;
	import classes.Global;	
	import classes.pet.Pet;
	import classes.rocks.BtnBar;
	import com.mttang.loader.SwfLoader;
	import com.mttang.event.JEvent;
	import com.mttang.common.*;
	import classes.savedata.RockmanData;
	import com.mttang.event.CommonEvent;
	import classes.utils.KeySets;
	import classes.utils.PopKeys;
	import classes.player.Fighter;
	import classes.bag.BagRockMan;
	import classes.audio.Audio;
	import classes.audio.AudioRockman;
	import classes.help.Help;
	import classes.help.HighScore;
	

	public class RockMain extends MovieClip {

		private var flashvars:*;                                             	//flashvar变量参数
		private var sharedData:Object;                                       	//共享对象
		private var _cursor:MouseIcon;											//鼠标游标
		private var id:int;														//
		private var createName:CreateName;                                    	//新建档案
		private var save_Data:Object;                                         	//临时共享对象数据
		private var startmc:StartMC;                                          	//开始游戏界面     
		private var showMan:*;                                                	//显示人物容器
		private var swfloader:SwfLoader;                                      	//加载类   
		private var rscurlArr:Array;                                          	//加载资源存放数组  
		private var _btnBar:BtnBar;                                           	//按钮容器
		private var _gamePlane:GamePlane;                                     	//游戏容器 
		private var _ks:KeySets;                                              	//键盘值
		//加载相关的外部资源
		private var _storeTg:*;                                               	//商店资源对象
		private var _bagTg:BagRockMan;										  	//背包
		private var _showPet:*													//宠物显示 
		private var _highScore:*												//显示高分榜
		private var _confg:*;													//游戏配置
		private var _help:*;													//游戏帮助
		private var _intr:*;													//游戏介绍
		
		//游戏层
		private var _uiLayer:Sprite;
		private var _btnLayer:Sprite;

		public function RockMain(){
			if (stage){
				initGame();
			} else {
				addEventListener(Event.ADDED_TO_STAGE, initGame);
			}
		}

		private function initGame(e:Event = null):void {
			flashvars = loaderInfo.parameters;
			removeEventListener(Event.ADDED_TO_STAGE, initGame);
			//鼠标游标
			//_cursor = new MouseIcon();
			addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);	
			//Mouse.hide();
			stage.addEventListener(Event.MOUSE_LEAVE, mouseLeaveHandler);
			//加载资源数据
			initGamedata();
		}

		private function mouseMoveHandler(e:MouseEvent):void {
			//_cursor.x = e.stageX;
			//_cursor.y = e.stageY;
			Mouse.show();
		}
		
		private function mouseLeaveHandler(e:Event):void {
			//mouseOutHandler(new MouseEvent(MouseEvent.MOUSE_MOVE));
		}

		//加载资源数据
		private function initGamedata():void {
			//创建层管理
			_uiLayer = new Sprite();
			_btnLayer = new Sprite();
			addChildAt(_uiLayer, numChildren);
			addChildAt(_btnLayer, numChildren);
			//addChildAt(_cursor,numChildren);
			
			rscurlArr = new Array();
			rscurlArr.push("sources/fla/shopMain.swf");
			rscurlArr.push("sources/fla/ui/ui.swf");
			rscurlArr.push("sources/fla/musicRockMan.swf");
			rscurlArr.push("sources/fla/petUi/petUi.swf");
			
			if (rscurlArr.length > 0){
				swfloader = new SwfLoader();
				swfloader.addEventListener(Event.COMPLETE, loadComHandler);
				swfloader.load(rscurlArr);
			}
		}

		private function loadComHandler(e:Event):void {
			removeChild(bg_loading);
			bg_loading = null;
			GameSetting.storeRsc = SwfLoader.autoGetLoader("sources/fla/shopMain.swf");
			GameSetting.uiRsc = SwfLoader.autoGetLoader("sources/fla/ui/ui.swf");
			GameSetting.audioRsc = SwfLoader.autoGetLoader("sources/fla/musicRockMan.swf");
			GameSetting.petRsc = SwfLoader.autoGetLoader("sources/fla/petUi/petUi.swf");
			gamePort();
			//背景音乐控制
			AudioRockman.play(Audio.START_MUSIC,true);
		}
		
		private function gamePort():void {
			bg_mc.btn_play.visible = true;
			bg_mc.btn_play.addEventListener(MouseEvent.CLICK, bgPlayClickHandler);
			//背景音效控制
			playBgSound();
		}
		
		private function playBgSound():void {
			bg_mc.bg.bg.earthquake_mc.addEventListener("earthquakeSoundEvent", earthquakeSoundHandler);
			bg_mc.bg.tujiu.addEventListener("tujiuSoundEvent", tujiuSoundHandler);
		}
		
		private function stopBgSound():void {
			bg_mc.bg.bg.earthquake_mc.removeEventListener("earthquakeSoundEvent", earthquakeSoundHandler);
			bg_mc.bg.tujiu.removeEventListener("tujiuSoundEvent", tujiuSoundHandler);
			AudioRockman.stop(Audio.EARTHQUAKE_NEW);
			AudioRockman.stop(Audio.CROW2_NEW);
		}
		
		private function earthquakeSoundHandler(e:Event):void {
			//trace("earthquake_sound");
			AudioRockman.play(Audio.EARTHQUAKE_NEW);
		}
		
		private function tujiuSoundHandler(e:Event):void {
			//trace("tujiuSoundHandler");
			AudioRockman.play(Audio.CROW2_NEW);
		}
		
		private function bgPlayClickHandler(e:MouseEvent):void {
			AudioRockman.play(Audio.MOUSECLICK_NEW);
			bg_mc.btn_play.removeEventListener(MouseEvent.CLICK, bgPlayClickHandler);
			bg_mc.btn_play.visible = false;
			
			//停止开始动画的声音
			stopBgSound();			
			createRockName();
		}

		private function createRockName():void {
			createName = new CreateName();
			_uiLayer.addChildAt(createName, _uiLayer.numChildren);
			createName.addEventListener(CommonEvent.CREATE_NAME_EVENT, createNameHandler);
			createName.addEventListener(CommonEvent.BACK_EVENT, createNameBackHandler);
		}
		
		private function createNameHandler(e:JEvent):void {
			createName.removeEventListener(CommonEvent.CREATE_NAME_EVENT, createNameHandler);
			createName.removeEventListener(CommonEvent.BACK_EVENT, createNameBackHandler);
			_uiLayer.removeChild(createName);
			createName = null;
			//开始界面ui设置
			showGameUI(e.info.id);
			id = e.info.id;
			GameSetting.index = id;
			//设置共享对象
			Global.getIns().stageRef = stage;
			Global.getIns().record = SharedObject.getLocal(GameSetting.saveSoData).data.dataRockman[e.info.id-1];
		}
		
		private function createNameBackHandler(e:Event):void {
			if (createName) {
				createName.removeEventListener(CommonEvent.CREATE_NAME_EVENT, createNameHandler);
				createName.removeEventListener(CommonEvent.BACK_EVENT, createNameBackHandler);
				_uiLayer.removeChild(createName);
				createName = null;
			}			
			gamePort();
		}

		//开始游戏设置界面		
		private function showGameUI(_id:int):void {
			//播放开始动画的声音
			playBgSound();
			//开始画面
			startmc = new StartMC();
			if (_btnBar) {
				_btnBar.show();
			}else {
				_btnBar = new BtnBar();
				_btnBar.x = GameSetting.stageWidth - _btnBar.width;
				_btnBar.y = GameSetting.stageHeight - _btnBar.height;
				_btnLayer.addChildAt(_btnBar, _btnLayer.numChildren);
				_btnBar.addEventListener("btnBarEvent", btnBarHandler);
			}
			_uiLayer.addChildAt(startmc, _uiLayer.numChildren);			
			showRockMan(_id);
		}
		
		private function showRockMan(_id:int):void {
			sharedData = SharedObject.getLocal(GameSetting.saveSoData);			
			if (sharedData.data.dataRockman[_id-1]!=null) {
				var tmpCls = getDefinitionByName(sharedData.data.dataRockman[_id-1].user.manid) as Class;
				if (showMan) {
					startmc.removeChild(showMan);
					showMan = null;
				}
				showMan = new tmpCls();
				showMan.x = 80;
				showMan.y = 200;
				startmc.addChildAt(showMan, startmc.numChildren);
			}						
		}
		
		private function btnBarHandler(e:JEvent):void {
			AudioRockman.play(Audio.MOUSECLICK_NEW);
			e.stopPropagation();
			backFunc();
			//trace(e.info.type);
			var tmpStr:String = e.info.type;
			switch(tmpStr) {
				case "exit":
					backFunc(null, true);
					//停止开始动画的声音
					stopBgSound();					
					break;
				case "help":
					//helpFunc();
					break;
				case "play":
					showPetFunc();
					/*
					backFunc();
					if (showMan) {
						startmc.removeChild(showMan);
						showMan = null;
					}
					if (startmc) {
						_uiLayer.removeChild(startmc);
						startmc = null;
					}
					*/
					break;
				case "intr":
					//intrFunc();
					break;
				case "score":
					scoreFunc();
					break;		
				case "store":
					//storeFunc();
					break;	
				case "config":
					//configFunc();
					break;
				case "bag":
					//bagFunc();
					break;	
				default:
					break;
			}
		}

		private function backFunc(e:*= null, _flag:Boolean = false):void {
		
			if (_gamePlane) {
				_gamePlane.rock_close();
				_uiLayer.removeChild(_gamePlane);
				_gamePlane = null;
			}
			
			if (_showPet) {
				_showPet.rock_close();
				_uiLayer.removeChild(_showPet);
				_showPet = null;
			}
			
			if (_highScore) {
				_uiLayer.removeChild(_highScore);
				_highScore = null;
			}
			
			if (showMan) {
					startmc.removeChild(showMan);
					showMan = null;
				}
				if (startmc) {
					_uiLayer.removeChild(startmc);
					startmc = null;
				}	
						
			if (_flag) {
				createRockName();
				_btnBar.hide();			
			}			
		}
		
		private function showPetFunc():void {	
			//停止开始动画的声音
			stopBgSound();
			AudioRockman.stop(Audio.START_MUSIC);//停止背景音乐			
			backFunc();
			_btnBar.hide();
			
			var _petCls:Loader = GameSetting.petRsc;
			_showPet = _petCls.content;
			_uiLayer.addChildAt(_showPet, _uiLayer.numChildren);
			trace(sharedData.data.dataRockman);
			_showPet.setData(sharedData.data.dataRockman[GameSetting.index-1]);
			_showPet.addEventListener(CommonEvent.BACK_EVENT, petBackHandler);
			_showPet.addEventListener(CommonEvent.PLAY_GAME_EVENT,playGameHandler);
		}
		
		private function petBackHandler(e:Event):void {
			AudioRockman.play(Audio.START_MUSIC, true);
			backFunc(null,true);
		}
		
		private function playGameHandler(e:JEvent):void {	
			backFunc();
			_gamePlane = new GamePlane();
			_uiLayer.addChildAt(_gamePlane, _uiLayer.numChildren);
			_gamePlane.setData(sharedData.data.dataRockman[id-1]);
			_ks = new KeySets(81,87,69,82,84,49,50,51,27);
			PopKeys.initStage(stage);
			_gamePlane.keySets = _ks;
			_gamePlane.addEventListener(CommonEvent.BACK_EVENT, backHandler);
			_gamePlane.addEventListener(CommonEvent.GAME_OVER_EVENT, gameOverHandler);
		}
		
		private function backHandler(e:Event):void {
			backFunc();
			showPetFunc();
		}
		
		/*
		private function helpFunc():void {
			if (!_help) {
				_help = new Help();
				_uiLayer.addChildAt(_help, _uiLayer.numChildren);
				_help.showHelp();
				_help.addEventListener(CommonEvent.BACK_EVENT, backFunc);
			}	
		}
		
		private function intrFunc():void {
			if (!_intr) {
				_intr = new Intr();						
				_uiLayer.addChildAt(_intr, _uiLayer.numChildren);
				_intr.showIntr();
				_intr.addEventListener(CommonEvent.BACK_EVENT, backFunc);
			}	
		}
		
		private function configFunc():void {
			//trace("config");
		}
		*/
		
		private function scoreFunc():void {
			if (!_highScore) {
				_highScore = new HighScore();		
				_uiLayer.addChildAt(_highScore, _uiLayer.numChildren);
				_highScore.showScore();
				_highScore.addEventListener(CommonEvent.BACK_EVENT, backFunc);
			}
		}
		/*
		private function storeFunc():void {	
			if (!_storeTg) {
				var _storeCls:Loader = GameSetting.storeRsc;						
				_storeTg = _storeCls.content;			
				_uiLayer.addChildAt(_storeTg, _uiLayer.numChildren);			
				_storeTg.setData(sharedData.data.dataRockman[id - 1]);
				_storeTg.addEventListener(CommonEvent.BACK_EVENT,backFunc);
			}
		}
		
		private function bagFunc():void {
			if (!_bagTg) {
				_bagTg = new BagRockMan();
				_uiLayer.addChildAt(_bagTg, _uiLayer.numChildren);
				_bagTg.show(sharedData.data.dataRockman[id - 1]);
				_bagTg.addEventListener(CommonEvent.BACK_EVENT,backFunc);
			}			
		}
		*/
		
		private function gameOverHandler(e:Event):void {
			
		}
		
/**********************************************************************************************/
	}
}